package com.geo.keyword.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geo.keyword.entity.Keyword;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 关键词Mapper接口
 */
@Mapper
public interface KeywordMapper extends BaseMapper<Keyword> {
    
    /**
     * 根据用户ID查询关键词列表
     */
    @Select("SELECT * FROM keywords WHERE user_id = #{userId} AND deleted = 0 ORDER BY created_at DESC")
    List<Keyword> findByUserId(@Param("userId") String userId);
    
    /**
     * 根据状态查询关键词
     */
    @Select("SELECT * FROM keywords WHERE status = #{status} AND deleted = 0")
    List<Keyword> findByStatus(@Param("status") String status);
    
    /**
     * 更新关键词优化状态
     */
    @Update("UPDATE keywords SET optimization_status = #{status}, updated_at = NOW() WHERE id = #{keywordId}")
    int updateOptimizationStatus(@Param("keywordId") String keywordId, @Param("status") String status);
    
    /**
     * 批量更新关键词状态
     */
    @Update("UPDATE keywords SET status = #{status}, updated_at = NOW() WHERE id IN (${keywordIds})")
    int batchUpdateStatus(@Param("keywordIds") String keywordIds, @Param("status") String status);
    
    /**
     * 查询热门关键词
     */
    @Select("SELECT * FROM keywords WHERE deleted = 0 ORDER BY search_volume DESC LIMIT #{limit}")
    List<Keyword> findTopKeywords(@Param("limit") int limit);
    
    /**
     * 根据难度范围查询关键词
     */
    @Select("SELECT * FROM keywords WHERE difficulty >= #{minDifficulty} AND difficulty <= #{maxDifficulty} AND deleted = 0")
    List<Keyword> findByDifficultyRange(@Param("minDifficulty") Double minDifficulty, @Param("maxDifficulty") Double maxDifficulty);
}